<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <link rel="stylesheet" href="../css/orderset.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>知识库管理<small>食品知识库</small></h1>
            </div>
            <hr />
            <div class="boxMain">
                    <el-button style="margin-bottom: 20px;margin-right: 20px" type="primary" @click="downloadTemplate()">模板下载</el-button>
                    <el-upload action="/uploadknowledge/upload.do"
                               name="excelFile"
                               :show-file-list="false"
                               :on-success="handleSuccess"
                               :before-upload="beforeUpload">
                        <el-button type="primary">上传文件</el-button>
                    </el-upload>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="单位/食物名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
<!--
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
-->
                        <el-table-column prop="id" width="100px" label="序号"></el-table-column>
                        <el-table-column prop="foodName" label="食物名称" align="center"></el-table-column>
                        <el-table-column prop="foodProtein" label="蛋白质含量" align="center"></el-table-column>
                        <el-table-column prop="foodFat" label="脂肪含量" align="center"></el-table-column>
                        <el-table-column prop="foodKcal" label="热量" align="center"></el-table-column>
                        <el-table-column prop="foodUnit" label="单位" align="center"></el-table-column>
                        <el-table-column prop="foodWeight" label="水分含量" align="center"></el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
            },
            created() {
                this.findPage();//VUE对象初始化完成后调用分页查询方法，完成分页查询
            },
            methods: {
                handleCurrentChange(currentPage) {
                    //设置最新的页码
                    this.pagination.currentPage = currentPage;
                    //重新调用findPage方法进行分页查询
                    this.findPage();
                },
                findPage() {
                    //封装分页参数
                    var param = {
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    };
                    //发送ajax请求，提交分页相关参数
                    axios.post("/uploadknowledge/findPage.do",param).then((res) => {
                        //为VUE对象的模型数据赋值，基于双向数据绑定展示到页面
                        this.dataList = res.data.rows;//当前页展示的数据集合
                    this.pagination.total = res.data.total;//总数据量
                });
                },
                beforeUpload(file) {
                    const isXLS = file.type === 'application/vnd.ms-excel';
                    if (isXLS) {
                        return true;
                    }
                    const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                    if (isXLSX) {
                        return true;
                    }
                    this.$message.error('上传文件只能是xls或者xlsx格式!');
                    return false;
                },
                //下载模板文件
                downloadTemplate() {
                    window.location.href = "../../template/foodknowledge_template.xls";
                },
                //上传成功提示
                handleSuccess(response) {
                    if (response.flag) {
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                    } else {
                        this.$message.error(response.message);
                    }
                    console.log(response, fileList);
                }
            }
        })
    </script>
</html>